package com.woniuxy.stage3demo.mapper.chenyuxuanMapper;


import com.woniuxy.stage3demo.bean.chenyuxuanBean.DeptBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;


/**
 * 部门查询mapper方法
 */
@Mapper
public interface DeptMapper {

    /**
     * 分页查询所有部门
     *
     * @return
     */
    @Results({
            @Result(id = true,property = "id",column = "id",javaType = int.class),
            @Result(property = "deptName",column = "dept_name",javaType =String.class),
            @Result(property = "deptDesc",column = "dept_desc",javaType = String.class),
            @Result(property = "createTime",column = "create_time",javaType = Date.class),
            @Result(property = "status",column = "status",javaType = String.class),
            @Result(property = "delFlag",column = "del_flag",javaType = String.class)
    })
    @Select("<script>" +
            "select d.* from dept as d" +
            "<where> " +
            " d.del_flag=0 " +
            "<if test='d.deptName!=\"\" and d.deptName !=null '>" +
            "and dept_name = #{d.deptName} " +
            "</if> " +
            "<if test='d.deptDesc!=\"\" and d.deptDesc !=null'>" +
            "and dept_desc like concat('%',#{d.deptDesc},'%') " +
            "</if>" +
            "<if test='p.sort != null and p.sortOrder != null'>" +
            "order by ${p.sort} ${p.sortOrder}" +
            "</if>" +
            "</where>" +
            "</script>")
    List<DeptBean> findAllDept(@Param("p") PageRequest pageRequest, @Param("d") DeptBean deptBean);


    /**
     * 添加部门
     * @param deptBean
     */
    @Options(
            useGeneratedKeys = true,//主键自增
            keyProperty = "a.id"
    )
    @Insert("insert into dept " +
            "(dept_name,dept_desc,create_time,status,del_flag)" +
            " values(#{a.deptName},#{a.deptDesc},now(),'1',#{a.delFlag})")
    int addDept(@Param("a") DeptBean deptBean);


    /**
     * 删除部门改变删除标记del_flag变为1
     * @param id
     */
    @Update("update dept set del_flag = 1 where id = #{id}")
    void deleteDept(@Param("id") int id);

    /**
     * 根据ID查询部门信息
     *
     * @param id
     * @return
     */
    @Results({
            @Result(id = true,property = "id",column = "id",javaType = int.class),
            @Result(property = "deptName",column = "dept_name",javaType = String.class),
            @Result(property = "deptDesc",column = "dept_desc",javaType = String.class),
            @Result(property = "createTime",column = "create_time",javaType = Date.class),
            @Result(property = "status",column = "status",javaType = String.class),
            @Result(property = "delFlag",column = "del_flag",javaType = String.class)
    })
    @Select("select * from dept where  id = #{id}")
    DeptBean findOneById(@Param("id") int id);

    /**
     * 修改部门资料
     * @param deptBean
     */
    @Update("<script>" +
            "update dept" +
            "<set>" +
            "<if test='d.deptName != null'>" +
            " dept_name = #{d.deptName}," +
            "</if>"+
            "<if test='d.deptDesc != null'>" +
            " dept_desc = #{d.deptDesc}," +
            "</if>"+
            "<if test='d.createTime != null'>" +
            " create_time = #{d.createTime}," +
            "</if>"+
            "<if test='d.status != null'>" +
            " status = #{d.status}," +
            "</if>"+
            "del_flag='0'"+
            "where id = #{d.id}" +
            "</set>"+
            "</script>")
    int updateDept(@Param("d") DeptBean deptBean);

    /**
     * 查询所有
     * @return
     */
    @Select("select id,dept_name deptName from dept where del_flag = 0")
    List<DeptBean> findAll();

}
